package com.dana1.nos.impl;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.UUID;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dana1.nos.data.Address;
import com.dana1.nos.data.AddressExtension;
import com.dana1.nos.data.User;
import com.dana1.nos.old.ObjectService;
import com.dana1.nos.old.generator.TimeBasedUUIDGenerator;

public class TestAbstractObjectService
{
	protected ObjectService os = null;
	
	@Before
	public void setUp() throws Exception
	{
		os = new NullObjectService();
	}
	
	@Test
	public void normalFieldsWork()
	{
		Timestamp t = new Timestamp(System.currentTimeMillis());
		User u1 = os.newInstance(User.class);
		UUID uuid = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		u1.setTimeStamp(t);
		u1.setUserId(uuid);
		assertEquals(t, u1.getTimeStamp());
		assertSame(uuid, u1.getUserId());
	}
	

	@Test
	public void hashCodeWorks()
	{
		Timestamp t = new Timestamp(System.currentTimeMillis());
		UUID uuid = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		UUID uuid2 = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		
		User u1 = os.newInstance(User.class);
		u1.setTimeStamp(t);
		u1.setUserId(uuid);
		u1.setSecureObjectID(uuid2);
		int beforeCode = u1.hashCode();

		u1 = os.newInstance(User.class);
		u1.setTimeStamp(t);
		u1.setUserId(uuid);
		u1.setSecureObjectID(uuid2);
		u1.setUsername("lame");
		int afterCode = u1.hashCode();

		assertEquals(beforeCode,afterCode);
	}

	@Test
	public void equalsWorks()
	{
		UUID uuid = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		UUID uuid2 = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		User u1 = os.newInstance(User.class);
		u1.setTimeStamp(new Timestamp(System.currentTimeMillis()));
		u1.setUserId(uuid);
		u1.setSecureObjectID(uuid2);
		
		User u2 = os.newInstance(User.class);
		u2.setTimeStamp(u1.getTimeStamp());
		u2.setUserId(uuid);
		u2.setSecureObjectID(uuid2);
		u2.setUsername("lame");
		assertEquals(u1, u2); // should be equals since OK's are the same
	}


	@Test
	public void toStringWorks()
	{
		UUID uuid = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		UUID uuid2 = (UUID)new TimeBasedUUIDGenerator().nextValue(User.class);
		User u2 = os.newInstance(User.class);
		u2.setTimeStamp(new Timestamp(0l));
		u2.setUserId(uuid);
		u2.setSecureObjectID(uuid2);
		u2.setUsername("lame");
		assertEquals("User[{secureObjectID="+uuid2.toString()+", timeStamp=1969-12-31 16:00:00.0, userId="+uuid.toString()+", username=lame}]", u2.toString());
	}

	@Test
	public void generatedByFieldsWork()
	{
		User u = os.newInstance(User.class);
		assertNotNull(u.getTimeStamp());
		assertNotNull(u.getSecureObjectID());
		assertNotNull(u.getUserId());
	}

	@Test
	public void serializedCollectionsWork()
	{
		Address address = os.newInstance(Address.class);
		ArrayList<String> vals = new ArrayList<String>();
		vals.add("Line 1");
		address.setAddressLines(vals);
		ArrayList<String> vals2 = new ArrayList<String>();
		vals2.add("Line 1");
		assertEquals(vals2, address.getAddressLines());
		assertEquals(vals, address.getAddressLines());
	}

	@Test
	public void serializedCollectionsInExtendedInterfacesWork()
	{
		AddressExtension address = os.newInstance(AddressExtension.class);
		ArrayList<String> vals = new ArrayList<String>();
		vals.add("Line 1");
		address.setAddressLines(vals);
		ArrayList<String> vals2 = new ArrayList<String>();
		vals2.add("Line 1");
		assertEquals(vals2, address.getAddressLines());
		assertEquals(vals, address.getAddressLines());
	}

	@After
	public void tearDown() throws Exception
	{
	}

}
